package ModelLayer;
import java.util.ArrayList;
import java.util.Arrays;

/**
 * Vestbjerg project
 * @GroupB2 (UCN - Computer Science DMAJ0914) 
 */
public class ProductContainer
{
    // instance variables
    private static ProductContainer instance;
    private Product product;
    private ArrayList<Product> productList;
    
    /**
     * Constructor for objects of class ProductContainer
     */
    public ProductContainer()
    {
        productList = new ArrayList<Product>();
    }

    public static ProductContainer getInstance()
    {
        if(instance == null)
        {
            instance = new ProductContainer();
        }
        return instance;
    }

    public void addProduct(Product product) 
    {
        productList.add(product);
    }

    public Product findProduct(String barcode) 
    {
        Product returnProduct = null;
        for(Product product : productList) 
        {

            if(barcode.equals(product.getBarcode())) 
            {
                returnProduct = product;
            }
        }
        return returnProduct;
    }

    public void deleteProduct(Product product) 
    {
        if(productList.contains(product)) 
        {
            productList.remove(product);
        }
    }

    public ArrayList<String> productSort()
    {
        ArrayList<String> sortedProductList = new ArrayList<String>();
        Item[] productArr = new Item[productList.size()];

        productList.toArray(productArr);
        Arrays.sort(productArr);

        for(Item product : productArr)
        {
            sortedProductList.add(product.getBarcode());
        }

        return sortedProductList;
    }
    
    public ArrayList<Product> getProductList() 
    {
     return productList;
    }
}
